package com.wanghaibo.vgashow.climatecontrol;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.ecarx.xui.adaptapi.audio.audiofx.IHarmanEqualizer;
import com.ecarx.xui.adaptapi.car.Car;
import com.ecarx.xui.adaptapi.car.ICar;
import com.ecarx.xui.adaptapi.car.base.ICarFunction;
import com.ecarx.xui.adaptapi.car.hvac.IHvac;
import com.wanghaibo.vgashow.api.CarAdaptAPI;
import com.wanghaibo.vgashow.api.ICarAdaptAPI;
import com.wanghaibo.vgashow.api.SimpleIFunctionValueWatcher;

import io.reactivex.functions.Consumer;

import static com.ecarx.xui.adaptapi.car.hvac.IHvac.*;
import static com.ecarx.xui.adaptapi.vehicle.VehicleZone.ZONE_ALL;
import static com.ecarx.xui.adaptapi.vehicle.VehicleZone.ZONE_ROW_1_LEFT;
import static com.ecarx.xui.adaptapi.vehicle.VehicleZone.ZONE_ROW_1_RIGHT;
import static com.ecarx.xui.adaptapi.vehicle.VehicleZone.ZONE_ROW_2_LEFT;
import static com.ecarx.xui.adaptapi.vehicle.VehicleZone.ZONE_ROW_2_RIGHT;

public class ClimateControl {

    //    public static final int ON = 469;
    //    public static final int OFF = 470;
    public static final int VENT_01 = ZONE_ROW_1_LEFT;
    public static final int VENT_02 = ZONE_ROW_1_RIGHT;
    public static final int VENT_03 = ZONE_ROW_2_LEFT;
    public static final int VENT_04 = ZONE_ROW_2_RIGHT;
    private static final String TAG = "ClimateControl";
    private static final String TAG_observe="ClimateObserver";

    private Context mContext;

    //当前风速
    public int FanSpeedLevel;

//    ICar iCar;
//    ICarFunction iCarFunction;

//    private int powerStatus;//空调总开关
//    private int ventUpStatus;//出风模式：上
//    private int ventMidStatus;//出风模式：中
//    private int ventDownStatus;//出风模式：下
//    private int autoStatus;//自动
//    private int acStatus;//A/C
//    private int cycleStatus;//内外循环
//    private int rearStatus;//二排空调开关

    public ClimateControl(Context context) {
        mContext = context;
    }

    /**
     * init Car
     */
    public void initCar(){
        //CarAdaptAPI是单例类
        CarAdaptAPI.Companion.getINSTANCE().init(mContext);
    }


    /**
     * 设置出风口方向
     *
     * @param zone  出风口号
     * @param windX 横向控制系数，0~100间整数
     * @param windY 纵向控制系数，0~100间整数
     */
    public void setWindDirection(int zone, int windX, int windY) {

        switch (zone) {
            case VENT_01:
                //Todo
                CarAdaptAPI.Companion.getINSTANCE().setCustomizeFunctionValue(HVAC_FUNC_SWEEPING_HORIZONTAL_POS,zone,windX).subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        Log.d(TAG, "HVAC_FUNC_SWEEPING_HORIZONTAL_POS(ZONE_ROW_1_LEFT):"+String.valueOf(aBoolean));
                    }
                });
                CarAdaptAPI.Companion.getINSTANCE().setCustomizeFunctionValue(HVAC_FUNC_SWEEPING_VERTICAL_POS,zone,windY).subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        Log.d(TAG,"HVAC_FUNC_SWEEPING_VERTICAL_POS(ZONE_ROW_1_LEFT):"+aBoolean);
                    }
                });
                Toast.makeText(mContext, "ZONE 01 (" + windX + ", " + windY + ")", Toast.LENGTH_SHORT).show();
                break;
            case VENT_02:
                //Todo
                CarAdaptAPI.Companion.getINSTANCE().setCustomizeFunctionValue(HVAC_FUNC_SWEEPING_HORIZONTAL_POS,zone,windX).subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        Log.d(TAG, "HVAC_FUNC_SWEEPING_HORIZONTAL_POS(ZONE_ROW_1_RIGHT):"+String.valueOf(aBoolean));
                    }
                });
                CarAdaptAPI.Companion.getINSTANCE().setCustomizeFunctionValue(HVAC_FUNC_SWEEPING_VERTICAL_POS,zone,windY).subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        Log.d(TAG,"HVAC_FUNC_SWEEPING_VERTICAL_POS(ZONE_ROW_1_RIGHT):"+aBoolean);

                    }
                });
                Toast.makeText(mContext, "ZONE 02 (" + windX + ", " + windY + ")", Toast.LENGTH_SHORT).show();
                break;
            case VENT_03:
                //Todo
                CarAdaptAPI.Companion.getINSTANCE().setCustomizeFunctionValue(HVAC_FUNC_SWEEPING_HORIZONTAL_POS,zone,windX).subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        Log.d(TAG, "HVAC_FUNC_SWEEPING_HORIZONTAL_POS(ZONE_ROW_2_LEFT):"+String.valueOf(aBoolean));
                    }
                });
                CarAdaptAPI.Companion.getINSTANCE().setCustomizeFunctionValue(HVAC_FUNC_SWEEPING_VERTICAL_POS,zone,windY).subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        Log.d(TAG,"HVAC_FUNC_SWEEPING_VERTICAL_POS(ZONE_ROW_2_LEFT):"+aBoolean);

                    }
                });
                Toast.makeText(mContext, "ZONE 02 (" + windX + ", " + windY + ")", Toast.LENGTH_SHORT).show();
                break;
            case VENT_04:
                //Todo
                CarAdaptAPI.Companion.getINSTANCE().setCustomizeFunctionValue(HVAC_FUNC_SWEEPING_HORIZONTAL_POS,zone,windX);
                CarAdaptAPI.Companion.getINSTANCE().setCustomizeFunctionValue(HVAC_FUNC_SWEEPING_VERTICAL_POS,zone,windY);
                Toast.makeText(mContext, "ZONE 04 (" + windX + ", " + windY + ")", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }

    public int[] getWindDirection(int zone) {
        initCar();
//        iCarFunction.registerF
        int windX = 2;
        int windY = 3;
        return new int[]{ windX ,windY};
    }


    /**
     * 开启空调
     *
     * @return 开启是否成功
     */
    public boolean turnOn() {
        //Todo
        initCar();
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_POWER,ICarFunction.COMMON_VALUE_ON).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                Log.d(TAG, "HVAC_FUNC_POWER:"+String.valueOf(aBoolean));
            }
        });
//        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(IHvac.HVAC_FUNC_AC,ICarFunction.COMMON_VALUE_ON);
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_FAN_SPEED, FAN_SPEED_LEVEL_9).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                Log.d(TAG,"HVAC_FUNC_FAN_SPEED:"+aBoolean);
            }
        });
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_DIRECTION_MODE, DIRECTION_MODE_CUSTOM).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                Log.d(TAG,"HVAC_FUNC_DIRECTION_MODE:"+aBoolean);
            }
        });

        //watcher
        initData();


        return true;
    }

    /**
     * 关闭空调
     *
     * @return 关闭是否成功
     */
    public boolean turnOff() {
        //Todo
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_POWER,ICarFunction.COMMON_VALUE_OFF).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean==true){
                    Log.d(TAG,"HVAC_FUNC_POWER关闭成功");
                }else {
                    Log.d(TAG,"HVAC_FUNC_POWER关闭失败");
                }
            }
        });
        CarAdaptAPI.Companion.getINSTANCE().destroy(true);
        return true;
    }

//    public boolean autoOn() {
//        //Todo
//        Toast.makeText(mContext, "自动空调开启成功", Toast.LENGTH_SHORT).show();
//        return true;
//    }
//
//    public boolean autoOff() {
//        //Todo
//        Toast.makeText(mContext, "自动空调关闭成功", Toast.LENGTH_SHORT).show();
//        return true;
//    }

    /**
     * 开启A/C
     *
     * @return 开启是否成功
     */
    public boolean acOn() {
        //Todo
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_AC,ICarFunction.COMMON_VALUE_ON).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean==true){
                    Toast.makeText(mContext, "A/C开启成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(mContext, "A/C开启失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return true;
    }

    /**
     * 关闭A/C
     *
     * @return 关闭是否成功
     */
    public boolean acOff() {
        //Todo
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_AC,ICarFunction.COMMON_VALUE_OFF).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean==true){
                    Toast.makeText(mContext, "A/C关闭成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(mContext, "A/C关闭失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return true;
    }

    /**
     * 开启内循环
     *
     * @return 开启是否成功
     */
    public boolean cycleInOn() {
        //Todo
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_CIRCULATION, CIRCULATION_INNER).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean==true){
                    Toast.makeText(mContext, "内循环开启成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(mContext, "内循环开启失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return true;
    }

    /**
     * 开启外循环
     *
     * @return 开启是否成功
     */
    public boolean cycleExOn() {
        //Todo
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_CIRCULATION, CIRCULATION_INNER).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean==true){
                    Toast.makeText(mContext, "外循环开启成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(mContext, "外循环开启失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return true;
    }

    /**
     * 开启后排空调
     *
     * @return 开启是否成功
     */
    public boolean rearOn() {
        //Todo
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_AUTO_SECOND_ROW_CLIMATE,ICarFunction.COMMON_VALUE_ON).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean==true){
                    Toast.makeText(mContext, "后排空调开启成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(mContext, "后排空调开启失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return true;
    }

    /**
     * 关闭后排空调
     *
     * @return 关闭是否成功
     */
    public boolean rearOff() {
        //Todo
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_AUTO_SECOND_ROW_CLIMATE,ICarFunction.COMMON_VALUE_OFF).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean==true){
                    Toast.makeText(mContext, "后排空调关闭成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(mContext, "后排空调关闭失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return true;
    }

    /**
     * 开启上出风口
     *
     * @return 开启是否成功
     */
    public boolean ventUpOn() {
        //Todo
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_BLOWING_MODE, BLOWING_MODE_FRONT_WINDOW).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean==true){
                    Toast.makeText(mContext, "上出风口开启成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(mContext, "上出风口开启失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return true;
    }

    /**
     * 关闭上出风口
     *
     * @return 关闭是否成功
     */
    public boolean ventUpOff() {
        //Todo
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_BLOWING_MODE, BLOWING_MODE_OFF).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean==true){
                    Toast.makeText(mContext, "上出风口关闭成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(mContext, "上出风口关闭失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return true;
    }

    /**
     * 开启中出风口
     *
     * @return 开启是否成功
     */
    public boolean ventMidOn() {
        //Todo
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_BLOWING_MODE, BLOWING_MODE_FACE).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean==true){
                    Toast.makeText(mContext, "中出风口开启成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(mContext, "中出风口开启失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return true;
    }

    /**
     * 关闭中出风口
     *
     * @return 关闭是否成功
     */
    public boolean ventMidOff() {
        //Todo
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_BLOWING_MODE, BLOWING_MODE_OFF).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean==true){
                    Toast.makeText(mContext, "中出风口关闭成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(mContext, "中出风口关闭失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return true;
    }

    /**
     * 开启下出风口
     *
     * @return 开启是否成功
     */
    public boolean ventDownOn() {
        //Todo
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_BLOWING_MODE, BLOWING_MODE_LEG).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean==true){
                    Toast.makeText(mContext, "下出风口开启成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(mContext, "下出风口开启失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        Toast.makeText(mContext, "下出风口开启成功", Toast.LENGTH_SHORT).show();
        return true;
    }

    /**
     * 关闭下出风口
     *
     * @return 关闭是否成功
     */
    public boolean ventDownOff() {
        //Todo
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_BLOWING_MODE, BLOWING_MODE_OFF).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean==true){
                    Toast.makeText(mContext, "下出风口关闭成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(mContext, "下出风口关闭失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return true;
    }

    /**
     * 风速调节
     *
     * @return 风速降低
     */
    public boolean FanSpeedDecrease() {
        CarAdaptAPI.Companion.getINSTANCE().setFunctionValue(HVAC_FUNC_FAN_SPEED,(FanSpeedLevel-1)).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean==true){
                    Toast.makeText(mContext, "风速降低成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(mContext, "风速降低失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return true;
    }

    /**
     * 风速调节
     *
     * @return 风速增大
     */
    public boolean FanSpeedIncrease(){
        return true;
    }

    /**
     * 风速大小
     */
    public void FanSpeedShow(){
        
    }

    //注册相关监听
    public void initData(){
        CarAdaptAPI.Companion.getINSTANCE().observeCarFunctionCustomize(watcher, HVAC_FUNC_SWEEPING_VERTICAL_POS);
        CarAdaptAPI.Companion.getINSTANCE().observeCarFunctionCustomize(watcher, HVAC_FUNC_FAN_SPEED);
        //CarAdaptAPI.Companion.getINSTANCE().observeCarFunctionCustomize(watcher,IHvac.HVAC_FUNC_SWEEPING_HORIZONTAL_POS);
    }

    //watcher监听
    public final ICarFunction.IFunctionValueWatcher watcher = new SimpleIFunctionValueWatcher() {
        @Override
        public void onFunctionChanged(int function) {
            super.onFunctionChanged(function);
        }


        @Override
        public void onFunctionValueChanged(int function, int zone, int value) {
            super.onFunctionValueChanged(function, zone, value);
            //针对状态变更
            switch (function) {
                case HVAC_FUNC_FAN_SPEED:
                    Toast.makeText(mContext,"当前风速为:"+value,Toast.LENGTH_SHORT).show();
                    FanSpeedLevel=value;
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onCustomizeFunctionValueChanged(int function, int zone, float value) {
            super.onCustomizeFunctionValueChanged(function, zone, value);
            Log.d(TAG_observe, "" + function + ":" + zone + ":" + value);
            switch (function) {
                case HVAC_FUNC_SWEEPING_VERTICAL_POS:
                    if (zone == VENT_01) {
                        //TODO
                        //获取到Vertical的风向的Value值，并操作
                        Log.d(TAG_observe, "HVAC_FUNC_SWEEPING_VERTICAL_POS(zone_1_left)的value值：" + value);
                    } else if (zone == VENT_02) {
                        Log.d(TAG_observe, "HVAC_FUNC_SWEEPING_VERTICAL_POS(zone_1_right)的value值：" + value);
                    }
                    break;
                case HVAC_FUNC_SWEEPING_HORIZONTAL_POS:
                    if (zone == VENT_01) {
                        //TODO
                        Log.d(TAG_observe, "HVAC_FUNC_SWEEPING_HORIZONTAL_POS(zone_1_left)的value值：" + value);
                    } else if (zone == VENT_02) {
                        Log.d(TAG_observe, "HVAC_FUNC_SWEEPING_HORIZONTAL_POS(zone_1_right)的value值：" + value);
                    }
                    break;
                default:
                    break;
            }
        }
    };
}
